package co.yixiang.modules.system.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.config.DataScope;
import co.yixiang.modules.system.domain.YxSales;
import co.yixiang.modules.system.service.DeptService;
import co.yixiang.modules.system.service.YxSalesService;
import co.yixiang.modules.system.service.dto.YxSalesDTO;
import co.yixiang.modules.system.service.dto.YxSalesQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author frq
 * @date 2020-05-25
 */
@Api(tags = "业务员管理管理")
@RestController
@RequestMapping("/api/yxSales")
public class YxSalesController {

    private final YxSalesService yxSalesService;
    private final DataScope dataScope;
    private final DeptService deptService;
    private final PasswordEncoder passwordEncoder;

    public YxSalesController(YxSalesService yxSalesService, DataScope dataScope, DeptService deptService, PasswordEncoder passwordEncoder) {
        this.yxSalesService = yxSalesService;
        this.dataScope = dataScope;
        this.deptService = deptService;
        this.passwordEncoder = passwordEncoder;
    }
    @Log("导出业务员数据")
    @ApiOperation("导出业务员数据")
    @PostMapping(value = "/download")
    public void download(HttpServletResponse response,@RequestBody YxSalesQueryCriteria criteria) throws IOException {
        yxSalesService.download(yxSalesService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询业务员管理")
    @ApiOperation("查询业务员管理")
    public ResponseEntity<Object> getYxSaless(YxSalesQueryCriteria criteria, Pageable pageable) {
        if (criteria.getDeptId() != null) {
            Set<Long> deptSet = new HashSet<>();
            deptSet.add(criteria.getDeptId());
            deptSet.addAll(dataScope.getDeptChildren(deptService.findByPid(criteria.getDeptId())));
            criteria.setDeptIds(deptSet);
        }
        return new ResponseEntity<>(yxSalesService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增业务员管理")
    @ApiOperation("新增业务员管理")
    public ResponseEntity<Object> create(@Validated @RequestBody YxSalesDTO resources) {
        yxSalesService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改业务员管理")
    @ApiOperation("修改业务员管理")
    public ResponseEntity<Object> update(@Validated @RequestBody YxSalesDTO resources) {
        yxSalesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除业务员管理")
    @ApiOperation("删除业务员管理")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxSalesService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 通过部门ID查询业务员
     *
     * @param deptId
     * @return
     */
    @Log("/查询业务员")
    @ApiOperation("查询业务员")
    @GetMapping("/findDetpListUser")
    public ResponseEntity findDetpListUser(@PathVariable @RequestParam String deptId) {
        List<YxSales> list = yxSalesService.findDetpListUser(deptId);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    /**
     * 通过用户账户查询业务员
     *
     * @param userName
     * @return
     */
    @Log("/查询业务员")
    @ApiOperation("查询业务员")
    @GetMapping("/findUserNameUser")
    public ResponseEntity findUserNameUser(@PathVariable @RequestParam String userName) {
        YxSales sales = yxSalesService.findUserNameUser(userName);
        Map<String, String> map = new HashMap<>();
        if (sales != null) {
            map.put("msg", "已存在");
        }
        map.put("msg", "不已存");
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /**
     * 修改密码
     *
     * @param resources
     * @return
     */
    @Log("/修改密码")
    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public ResponseEntity updatePassword(@Validated @RequestBody YxSalesDTO resources) {
        resources.setPwdPlaintext(resources.getPassword());
        resources.setPassword(passwordEncoder.encode(resources.getPassword()));
        yxSalesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}